<template>
	<view class="container">
		<!-- 头部 -->
		<!-- <view class="header">购物车</view> -->
		<!-- 地址 -->
		<view class="site" v-if="JSON.stringify(cartStore.selectSiteData) !== '{}' && cartStore.cartData.length > 0"
			@click="cartStore.goSite()">
			<image src="/static/images/dz.png" class="leftimg" />
			<view class="text">
				<view class="big-site">
					<view class="province">{{cartStore.selectSiteData.province}}</view>
					<view class="city">{{cartStore.selectSiteData.city}}</view>
					<view class="country">{{cartStore.selectSiteData.county}}</view>
					<view class="grade" v-if="cartStore.selectSiteData.isDefault">默认</view>
				</view>
				<view class="detail-site">{{cartStore.selectSiteData.addressDetail}}</view>
				<view class="userinfo">
					<view class="username">{{cartStore.selectSiteData.name}}</view>
					<view class="usernumber">{{cartStore.selectSiteData.tel}}</view>
				</view>
			</view>
			<image src="/static/images/arrowright.png" class="rightimg" />
		</view>
		<!-- 添加地址 -->
		<view class="addSite" v-if="JSON.stringify(cartStore.selectSiteData) === '{}' && cartStore.cartData.length > 0"
			@click="cartStore.goSite()">
			<image src="/static/images/add.png" class="leftimg" />
			<view class="text">选择收货地址</view>
			<image src="/static/images/arrowright.png" class="rightimg" />
		</view>

		<!-- 商品列表 -->
		<view class="good-list">
			<view class="good-item" v-for="item in cartStore.cartData" :key="item.id">
				<checkbox-group @change="select(item.id)">
					<checkbox class="checkbox" :checked="item.selected" />
				</checkbox-group>
				<image :src="item.imgurl" />
				<view class="text">
					<view class="title">{{item.title}}</view>
					<view class="down">
						<view class="price">￥{{item.price}}.00</view>
						<uni-number-box :min="1" v-model="item.count"></uni-number-box>
						<view class="del" @click="del(item.id)">删除</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 结算 -->
		<view class="final-box" v-if="cartStore.cartData.length > 0">
			<view class="notice">默认只支持微信支付</view>
			<view class="down">
				<view class="count">共{{cartStore.countAll}}件商品</view>
				<view class="all">合计:</view>
				<view class="price">￥{{cartStore.priceAll}}.00</view>
				<view class="close" @click="commit">去结算付款</view>
			</view>
		</view>

		<view class="none" v-if="cartStore.cartData.length === 0">
			<image src="/static/images/nocart.png" />
			<view class="big-title">
				购物车竟然是空的
			</view>
			<view class="small-title">
				再忙，也要记得买点什么犒劳自己
			</view>
			<view class="gohome" @click="homeStore.goHome()">
				去首页逛逛
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		computed
	} from 'vue';
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app';
	import {
		useCartStore,
		useLoginStore,
		useHomeStore
	} from '../../stores/store.js';

	const cartStore = useCartStore()
	const loginStore = useLoginStore()
	const homeStore = useHomeStore()

	onLoad(() => {
		cartStore.getSiteData()
	})

	onShow(() => {
		if (!loginStore.Token) {
			loginStore.goLogin('cart')
			uni.showToast({
				title: '请先登录',
				icon: 'error'
			})
		}
		cartStore.getCartPageData()
		cartStore.updateTabBarBadge()
	})

	const select = (id) => {
		cartStore.cartData.forEach(item => {
			if (item.id === id) {
				item.selected = !item.selected
			}
		})
	}
	// 删除购物商品
	const del = (id) => {
		uni.showModal({
			title: '确定删除吗',
			success: function(res) {
				if (res.confirm) {
					let index = cartStore.cartData.findIndex(item => item.id === id)
					cartStore.cartData.splice(index, 1)
					cartStore.updateTabBarBadge()
				}
			}
		});
	}

	// 结算
	const commit = async () => {
		if (cartStore.selectSiteData === {}) {
			uni.showToast({
				title: '请先选择收货地址',
				icon: 'error'
			})
			return
		}
		// 跳转订单详情页
		uni.navigateTo({
			url: '/pages/orderDetail/orderDetail',
			animationType: 'none'
		})
		// 提交订单请求
		await cartStore.submitOrder()

		// 清空购物车
		cartStore.cartData = []
	}
</script>

<style lang="scss">
	page {}

	.container {
		// background-color: #f0eded;
		height: 100vh;

		/* 头部 */
		// .header {
		// 	width: px2rpx(375);
		// 	height: px2rpx(46);
		// 	display: flex;
		// 	justify-content: center;
		// 	align-items: center;
		// 	background-color: #fff;
		// }

		// 地址
		.site {
			border-radius: px2rpx(5);
			width: px2rpx(365);
			height: px2rpx(80);
			display: flex;
			align-items: center;
			background-color: #fff;
			margin: px2rpx(6);

			.leftimg {
				width: px2rpx(35);
				height: px2rpx(35);
				margin: 0 px2rpx(5);
			}

			.rightimg {
				width: px2rpx(26);
				height: px2rpx(26);
				// margin-left: px2rpx(125);
			}

			.text {
				width: px2rpx(285);
				height: px2rpx(52);

				.big-site {
					display: flex;
					font-size: px2rpx(14);

					.grade {
						margin-left: px2rpx(2);
						width: px2rpx(35);
						height: px2rpx(20);
						display: flex;
						justify-content: center;
						align-items: center;
						background-color: #EE0A24;
						border-radius: px2rpx(2);
					}
				}

				.detail-site {
					font-size: px2rpx(14);
					font-weight: bold;
				}

				.userinfo {
					display: flex;
					font-size: px2rpx(14);

					.username {
						margin-right: px2rpx(10);
					}
				}
			}
		}

		// 添加地址
		.addSite {
			border-radius: px2rpx(5);
			width: px2rpx(365);
			height: px2rpx(80);
			display: flex;
			align-items: center;
			background-color: #fff;
			margin: px2rpx(6);
			font-size: px2rpx(14);

			.leftimg {
				width: px2rpx(30);
				height: px2rpx(30);
				border-radius: px2rpx(2);
				background-color: #1989fa;
				margin: 0 px2rpx(20);
			}

			.rightimg {
				width: px2rpx(20);
				height: px2rpx(20);
				margin-left: px2rpx(180);
			}
		}

		// 商品列表
		.good-list {
			margin-top: px2rpx(10);
			width: px2rpx(375);
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			.good-item {
				border-radius: px2rpx(5);
				width: px2rpx(367);
				height: px2rpx(105);
				background-color: #fff;
				display: flex;
				align-items: center;

				.checkbox {
					transform: scale(0.8);
					margin-left: px2rpx(5);
				}

				image {
					width: px2rpx(70);
					height: px2rpx(85);
					margin: px2rpx(5);
				}

				.text {
					width: px2rpx(250);
					height: px2rpx(85);
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					.title {
						width: px2rpx(250);
						font-size: px2rpx(14);
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}

					.down {
						display: flex;
						align-items: center;

						.price {
							font-size: px2rpx(16);
							color: #ff0000;
							margin-right: px2rpx(10);
						}

						.less,
						.add {
							display: flex;
							justify-content: center;
							align-items: center;
							width: px2rpx(28);
							height: px2rpx(28);
							margin: 0 px2rpx(5);
						}

						.count {
							width: px2rpx(32);
							height: px2rpx(28);
							display: flex;
							justify-content: center;
							align-items: center;
							background-color: #f2f3f5;
						}

						.del {
							display: flex;
							justify-content: center;
							align-items: center;
							width: px2rpx(50);
							height: px2rpx(32);
							font-size: px2rpx(12);
							color: #fff;
							border-radius: px2rpx(5);
							background-color: #ee0a24;
							margin-left: px2rpx(10);
						}
					}
				}
			}
		}

		// 结算框
		.final-box {
			position: absolute;
			left: 0;
			bottom: 0;
			z-index: 999;
			width: px2rpx(375);
			height: px2rpx(85);

			.notice {
				width: px2rpx(375);
				height: px2rpx(35);
				background-color: #fff7cc;
				color: #f56723;
				font-size: px2rpx(12);
				display: flex;
				align-items: center;
				padding-left: px2rpx(20);
			}

			.down {
				width: px2rpx(375);
				height: px2rpx(50);
				background-color: #fff;
				display: flex;
				align-items: center;

				.count {
					font-size: px2rpx(14);
					margin: 0 px2rpx(20);
				}

				.all {
					font-size: px2rpx(14);
				}

				.price {
					font-size: px2rpx(20);
					color: #ee0a24;
				}

				.close {
					width: px2rpx(110);
					height: px2rpx(40);
					background-color: #ee0a24;
					border-radius: px2rpx(20);
					display: flex;
					justify-content: center;
					align-items: center;
					color: #fff;
					font-size: px2rpx(14);
					margin-left: px2rpx(30);
				}
			}
		}

		// 无商品
		.none {
			width: px2rpx(375);
			height: px2rpx(300);
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			image {
				width: px2rpx(110);
				height: px2rpx(110);
			}

			.big-title {
				font-size: px2rpx(18);
				margin: px2rpx(20) 0;
			}

			.small-title {
				font-size: px2rpx(13);
				color: #999999;
			}

			.gohome {
				width: px2rpx(100);
				height: px2rpx(50);
				color: #999999;
				border: 1px solid #999999;
				display: flex;
				justify-content: center;
				align-items: center;
				margin: px2rpx(20) 0;
			}
		}
	}
</style>